package com.beiding.my.common.query;

import java.util.Arrays;
import java.util.Collection;

public class q {

    /***条件合并***/
    public static OuterExpression and(Expression... conditionExpressions) {
        OuterConnectorHasBody r = new OuterConnectorHasBody();
        r.setBody(conditionExpressions);
        r.setSymbol("and");
        return r;
    }

    public static OuterExpression or(Expression... conditionExpressions) {
        OuterConnectorHasBody r = new OuterConnectorHasBody();
        r.setBody(conditionExpressions);
        r.setSymbol("or");
        return r;
    }

    /****通用双目操作****/
    public static OuterExpression eq(String field, Object value) {
        OuterCompareBinary binaryCompare = new OuterCompareBinary();
        binaryCompare.setSymbol("=");
        binaryCompare.setField(field);
        binaryCompare.setValue(value);
        return binaryCompare;
    }

    public static OuterExpression ge(String field, Object value) {
        OuterCompareBinary binaryCompare = new OuterCompareBinary();
        binaryCompare.setSymbol(">=");
        binaryCompare.setField(field);
        binaryCompare.setValue(value);
        return binaryCompare;
    }

    public static OuterExpression gt(String field, Object value) {
        OuterCompareBinary binaryCompare = new OuterCompareBinary();
        binaryCompare.setSymbol(">");
        binaryCompare.setField(field);
        binaryCompare.setValue(value);
        return binaryCompare;
    }

    public static OuterExpression ne(String field, Object value) {
        OuterCompareBinary binaryCompare = new OuterCompareBinary();
        binaryCompare.setSymbol("!=");
        binaryCompare.setField(field);
        binaryCompare.setValue(value);
        return binaryCompare;
    }

    public static OuterExpression lt(String field, Object value) {
        OuterCompareBinary binaryCompare = new OuterCompareBinary();
        binaryCompare.setSymbol("<");
        binaryCompare.setField(field);
        binaryCompare.setValue(value);
        return binaryCompare;
    }

    public static OuterExpression le(String field, Object value) {
        OuterCompareBinary binaryCompare = new OuterCompareBinary();
        binaryCompare.setSymbol("<=");
        binaryCompare.setField(field);
        binaryCompare.setValue(value);
        return binaryCompare;
    }

    public static OuterExpression like(String field, Object value) {
        if (value == null) {
            return isNull(field);
        }
        OuterCompareBinary binaryCompare = new OuterCompareBinary();
        binaryCompare.setSymbol("like");
        binaryCompare.setField(field);
        binaryCompare.setValue("%" + value + "%");
        return binaryCompare;

    }

    public static OuterExpression likeLeft(String field, Object value) {
        if (value == null) {
            return isNull(field);
        }
        OuterCompareBinary binaryCompare = new OuterCompareBinary();
        binaryCompare.setSymbol("like");
        binaryCompare.setField(field);
        binaryCompare.setValue("%" + value);
        return binaryCompare;
    }

    public static OuterExpression likeRight(String field, Object value) {
        if (value == null) {
            return isNull(field);
        }
        OuterCompareBinary binaryCompare = new OuterCompareBinary();
        binaryCompare.setSymbol("like");
        binaryCompare.setField(field);
        binaryCompare.setValue(value + "%");
        return binaryCompare;
    }


    public static OuterExpression notLike(String field, Object value) {
        if (value == null) {
            return isNull(field);
        }
        OuterCompareBinary binaryCompare = new OuterCompareBinary();
        binaryCompare.setSymbol("not like");
        binaryCompare.setField(field);
        binaryCompare.setValue("%" + value + "%");
        return binaryCompare;
    }

    /*in操作*/
    public static OuterExpression in(String field, Object... fs) {
        return in(field, Arrays.asList(fs));
    }

    public static OuterExpression notIn(String field, Object... fs) {
        return notIn(field, Arrays.asList(fs));
    }

    public static OuterExpression in(String field, Collection value) {
        OuterCompareBinary binaryCompare = new OuterCompareBinary();
        binaryCompare.setSymbol("in");
        binaryCompare.setField(field);
        binaryCompare.setValue(value);
        return binaryCompare;
    }

    public static OuterExpression notIn(String field, Collection value) {
        OuterCompareBinary binaryCompare = new OuterCompareBinary();
        binaryCompare.setSymbol("in");
        binaryCompare.setField(field);
        binaryCompare.setValue(value);
        return binaryCompare;
    }


    /****特殊操作****/
    public static OuterExpression between(String field, Object from, Object to) {
        OuterCompareBetween between = new OuterCompareBetween();
        between.setSymbol("between");
        between.setField(field);
        between.setFrom(from);
        between.setTo(to);
        return between;
    }


    public static OuterExpression notBetween(String field, Object from, Object to) {
        OuterCompareBetween between = new OuterCompareBetween();
        between.setSymbol("not between");
        between.setField(field);
        between.setFrom(from);
        between.setTo(to);
        return between;
    }

    public static OuterExpression isNull(String field) {
        OuterCompareUnary unary = new OuterCompareUnary();
        unary.setSymbol("is null");
        unary.setField(field);
        return unary;
    }

    public static OuterExpression isNotNull(String field, Object value) {
        OuterCompareUnary unary = new OuterCompareUnary();
        unary.setSymbol("not null");
        unary.setField(field);
        return unary;
    }

    public static OuterExpression eq(FieldSelf fieldSelf, Object value) {
        OuterCompareBinary binaryCompare = new OuterCompareBinary();
        binaryCompare.setSymbol("=");
        binaryCompare.setField(fieldSelf._selfName);
        binaryCompare.setValue(value);
        return binaryCompare;
    }

    public static OuterExpression ge(FieldSelf fieldSelf, Object value) {
        OuterCompareBinary binaryCompare = new OuterCompareBinary();
        binaryCompare.setSymbol(">=");
        binaryCompare.setField(fieldSelf._selfName);
        binaryCompare.setValue(value);
        return binaryCompare;
    }

    public static OuterExpression gt(FieldSelf fieldSelf, Object value) {
        OuterCompareBinary binaryCompare = new OuterCompareBinary();
        binaryCompare.setSymbol(">");
        binaryCompare.setField(fieldSelf._selfName);
        binaryCompare.setValue(value);
        return binaryCompare;
    }

    public static OuterExpression ne(FieldSelf fieldSelf, Object value) {
        OuterCompareBinary binaryCompare = new OuterCompareBinary();
        binaryCompare.setSymbol("!=");
        binaryCompare.setField(fieldSelf._selfName);
        binaryCompare.setValue(value);
        return binaryCompare;
    }

    public static OuterExpression lt(FieldSelf fieldSelf, Object value) {
        OuterCompareBinary binaryCompare = new OuterCompareBinary();
        binaryCompare.setSymbol("<");
        binaryCompare.setField(fieldSelf._selfName);
        binaryCompare.setValue(value);
        return binaryCompare;
    }

    public static OuterExpression le(FieldSelf fieldSelf, Object value) {
        OuterCompareBinary binaryCompare = new OuterCompareBinary();
        binaryCompare.setSymbol("<=");
        binaryCompare.setField(fieldSelf._selfName);
        binaryCompare.setValue(value);
        return binaryCompare;
    }

    public static OuterExpression like(FieldSelf fieldSelf, Object value) {
        if (value == null) {
            return isNull(fieldSelf._selfName);
        }
        OuterCompareBinary binaryCompare = new OuterCompareBinary();
        binaryCompare.setSymbol("like");
        binaryCompare.setField(fieldSelf._selfName);
        binaryCompare.setValue("%" + value + "%");
        return binaryCompare;

    }

    public static OuterExpression likeLeft(FieldSelf fieldSelf, Object value) {
        if (value == null) {
            return isNull(fieldSelf._selfName);
        }
        OuterCompareBinary binaryCompare = new OuterCompareBinary();
        binaryCompare.setSymbol("like");
        binaryCompare.setField(fieldSelf._selfName);
        binaryCompare.setValue("%" + value);
        return binaryCompare;
    }

    public static OuterExpression likeRight(FieldSelf fieldSelf, Object value) {
        if (value == null) {
            return isNull(fieldSelf._selfName);
        }
        OuterCompareBinary binaryCompare = new OuterCompareBinary();
        binaryCompare.setSymbol("like");
        binaryCompare.setField(fieldSelf._selfName);
        binaryCompare.setValue(value + "%");
        return binaryCompare;
    }


    public static OuterExpression notLike(FieldSelf fieldSelf, Object value) {
        if (value == null) {
            return isNull(fieldSelf._selfName);
        }
        OuterCompareBinary binaryCompare = new OuterCompareBinary();
        binaryCompare.setSymbol("not like");
        binaryCompare.setField(fieldSelf._selfName);
        binaryCompare.setValue("%" + value + "%");
        return binaryCompare;
    }

    /*in操作*/
    public static OuterExpression in(FieldSelf fieldSelf, Object... fs) {
        return in(fieldSelf, Arrays.asList(fs));
    }

    public static OuterExpression notIn(FieldSelf fieldSelf, Object... fs) {
        return notIn(fieldSelf, Arrays.asList(fs));
    }

    public static OuterExpression in(FieldSelf fieldSelf, Collection value) {
        OuterCompareBinary binaryCompare = new OuterCompareBinary();
        binaryCompare.setSymbol("in");
        binaryCompare.setField(fieldSelf._selfName);
        binaryCompare.setValue(value);
        return binaryCompare;
    }

    public static OuterExpression notIn(FieldSelf fieldSelf, Collection value) {
        OuterCompareBinary binaryCompare = new OuterCompareBinary();
        binaryCompare.setSymbol("in");
        binaryCompare.setField(fieldSelf._selfName);
        binaryCompare.setValue(value);
        return binaryCompare;
    }


    /****特殊操作****/
    public static OuterExpression between(FieldSelf fieldSelf, Object from, Object to) {
        OuterCompareBetween between = new OuterCompareBetween();
        between.setSymbol("between");
        between.setField(fieldSelf._selfName);
        between.setFrom(from);
        between.setTo(to);
        return between;
    }


    public static OuterExpression notBetween(FieldSelf fieldSelf, Object from, Object to) {
        OuterCompareBetween between = new OuterCompareBetween();
        between.setSymbol("not between");
        between.setField(fieldSelf._selfName);
        between.setFrom(from);
        between.setTo(to);
        return between;
    }

    public static OuterExpression isNull(FieldSelf fieldSelf) {
        OuterCompareUnary unary = new OuterCompareUnary();
        unary.setSymbol("is null");
        unary.setField(fieldSelf._selfName);
        return unary;
    }

    public static OuterExpression isNotNull(FieldSelf fieldSelf, Object value) {
        OuterCompareUnary unary = new OuterCompareUnary();
        unary.setSymbol("not null");
        unary.setField(fieldSelf._selfName);
        return unary;
    }


}
